package org.bwf.study.servcie.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.bwf.obj.dto.IndexQueryListDTO;
import org.bwf.study.aop.RedisAnnotation;
import org.bwf.study.mapper.CategoryMapper;
import org.bwf.study.mapper.FilmMapper;
import org.bwf.study.mapper.TypeFilmRelMapper;
import org.bwf.study.mapper.TypeMapper;
import org.bwf.study.model.Category;
import org.bwf.study.model.Film;
import org.bwf.study.model.TypeFilmRel;
import org.bwf.study.servcie.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private TypeFilmRelMapper typeFilmRelMapper;
    @Autowired
    private FilmMapper filmMapper;
    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    @RedisAnnotation(Duration = 800)
    public IndexQueryListDTO Load() {

        IndexQueryListDTO indexQueryListDTO = new IndexQueryListDTO();

        // 正在热映
//        LambdaQueryWrapper<TypeFilmRel> nowPlayWrapper = Wrappers.lambdaQuery();
//        nowPlayWrapper.eq(TypeFilmRel::getTypeId, 1);
//        Page<TypeFilmRel> page = new Page<>(1, 8);
//        // 根据typeId获取关系列表
//        List<TypeFilmRel> typeFilmRelList = typeFilmRelMapper.selectPage(page, nowPlayWrapper).getRecords();
//        // filmIds集合用于存放关系列表中的FilmId
//        List<Integer> filmIds = new ArrayList<>();
//        for(var ids : typeFilmRelList){
//            filmIds.add(ids.getFilmId());
//        }
//        // 根据填充完的filmIds去查询film数据
//        LambdaQueryWrapper<Film> nowPlayFilmWrapper = Wrappers.lambdaQuery();
//        nowPlayFilmWrapper.in(Film::getFilmId, filmIds);
//        // 把查询到的正在热映的电影结合放入dto
//        indexQueryListDTO.setNowPlay(filmMapper.selectList(nowPlayFilmWrapper));
//        // 把查询到的正在热映的电影的总数量结合放入dto
//        indexQueryListDTO.setNowPlayCount((int)page.getTotal());
//
//        // 清空原先的filmId
//        filmIds.clear();
//        // 即将上映
//        LambdaQueryWrapper<TypeFilmRel> readyPlayWrapper = Wrappers.lambdaQuery();
//        readyPlayWrapper.eq(TypeFilmRel::getTypeId, 2);
//        Page<TypeFilmRel> readyPage = new Page<>(1, 8);
//        // 根据typeId获取关系列表
//        List<TypeFilmRel> typeReadyFilmRelList = typeFilmRelMapper.selectPage(readyPage, readyPlayWrapper).getRecords();
//
//        for(var ids : typeReadyFilmRelList){
//            filmIds.add(ids.getFilmId());
//        }
//        // 根据填充完的filmIds去查询film数据
//        LambdaQueryWrapper<Film> readyPlayFilmWrapper = Wrappers.lambdaQuery();
//        readyPlayFilmWrapper.in(Film::getFilmId, filmIds);
//        // 把查询到的正在热映的电影结合放入dto
//        indexQueryListDTO.setReadyPlay(filmMapper.selectList(readyPlayFilmWrapper));
//        // 把查询到的正在热映的电影的总数量结合放入dto
//        indexQueryListDTO.setReadyPlayCount((int)readyPage.getTotal());
//
//
//        filmIds.clear();
//        // 热播电影
//        LambdaQueryWrapper<TypeFilmRel> hotPlayWrapper = Wrappers.lambdaQuery();
//        hotPlayWrapper.eq(TypeFilmRel::getTypeId, 3);
//        Page<TypeFilmRel> hotPage = new Page<>(1, 7);
//        // 根据typeId获取关系列表
//        List<TypeFilmRel> typeHotFilmRelList = typeFilmRelMapper.selectPage(hotPage, hotPlayWrapper).getRecords();
//
//        for(var ids : typeHotFilmRelList){
//            filmIds.add(ids.getFilmId());
//        }
//        // 根据填充完的filmIds去查询film数据
//        LambdaQueryWrapper<Film> hotPlayFilmWrapper = Wrappers.lambdaQuery();
//        hotPlayFilmWrapper.in(Film::getFilmId, filmIds);
//        // 把查询到的正在热映的电影结合放入dto
//        indexQueryListDTO.setHotPlay(filmMapper.selectList(hotPlayFilmWrapper));

        // 最喜欢电影

        var nowPlayMap = getData(1, 8);
        indexQueryListDTO.setNowPlay((List<Film>) nowPlayMap.get("films"));
        indexQueryListDTO.setNowPlayCount((int)nowPlayMap.get("count"));

        var readyPlayMap = getData(2, 8);
        indexQueryListDTO.setReadyPlay((List<Film>) readyPlayMap.get("films"));
        indexQueryListDTO.setReadyPlayCount((int)readyPlayMap.get("count"));

        var hotPlayMap = getData(3, 7);
        indexQueryListDTO.setHotPlay((List<Film>) hotPlayMap.get("films"));


        LambdaQueryWrapper<Film> favFilmWrapper = Wrappers.lambdaQuery();
        favFilmWrapper.orderByDesc(Film::getFilmFavorite);
        Page<Film> favPage = new Page<>(1, 10);
        indexQueryListDTO.setFavPlay(filmMapper.selectPage(favPage, favFilmWrapper).getRecords());

        // top电影
        LambdaQueryWrapper<Film> topFilmWrapper = Wrappers.lambdaQuery();
        topFilmWrapper.orderByDesc(Film::getFilmPraise);
        Page<Film> topPage = new Page<>(1,10);
        indexQueryListDTO.setTopPlay(filmMapper.selectPage(topPage, topFilmWrapper).getRecords());

        // category
        Page<Category> categoryPage = new Page<>(1, 5);
        indexQueryListDTO.setCategories(categoryMapper.selectPage(categoryPage, null).getRecords());

        return indexQueryListDTO;
    }

    private Map<String, Object> getData (Integer typeId, Integer pageSize){
        LambdaQueryWrapper<TypeFilmRel> typeFilmRelLambdaQueryWrapper = Wrappers.lambdaQuery();
        typeFilmRelLambdaQueryWrapper.eq(TypeFilmRel::getTypeId, typeId);
        Page<TypeFilmRel> page = new Page<>(1, pageSize);
        List<TypeFilmRel> typeFilmRelList = typeFilmRelMapper.selectPage(page, typeFilmRelLambdaQueryWrapper).getRecords();
        List<Integer> filmIds = new ArrayList<>();
        for(var ids : typeFilmRelList){
            filmIds.add(ids.getFilmId());
        }
        LambdaQueryWrapper<Film> filmLambdaQueryWrapper = Wrappers.lambdaQuery();
        // select * from film where  film_id in (111,222,333,444)
        filmLambdaQueryWrapper.in(Film::getFilmId, filmIds);
        List<Film> filmList = filmMapper.selectList(filmLambdaQueryWrapper);
        int count = (int) page.getTotal();

        Map<String, Object> map = new HashMap<>();
        map.put("films", filmList);
        map.put("count", count);

        return map;
    }
}
